local skel = fk.CreateSkill {
  name = "rmt__jiechang",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target.phase == Player.Discard and not target.dead then
      local can_trigger = false
      local search = true
      player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if search and move.to == player then
            player.room:setPlayerMark(player, "@@rmt__jiechang1", 1)
            can_trigger = true
            search = false
          elseif search and move.from and move.to ~= player then
            player.room:setPlayerMark(player, "@@rmt__jiechang1", 0)
            search = false
          end
        end
      end, nil, Player.HistoryGame)
      return can_trigger
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ex_nihilo = Fk:cloneCard("ex_nihilo")
    local targets = table.filter(room:getAllPlayers(), function (p)
      return not player:isProhibited(p, ex_nihilo)
    end)
    if #targets < 2 then return false end
    local to = room:askToChoosePlayers(player, {
      min_num = 2,
      max_num = 2,
      targets = targets,
      skill_name = skel.name,
      prompt = "#rmt__jiechang_active1",
      cancelable = true,
    })
    if #to == 2 then
      event:setCostData(self, {tos = to})
      return true
    else
      player.room:setPlayerMark(player, "@@rmt__jiechang1", 0)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.room:setPlayerMark(player, "@@rmt__jiechang1", 0)
    local to = event:getCostData(self).tos
    player:turnOver()
    if not player.dead then
      room:useVirtualCard("ex_nihilo", nil, player, to, skel.name, true)
    end
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target.phase == Player.Discard and not target.dead then
      local can_trigger = false
      local search = true
      player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if search and move.from == player then
            player.room:setPlayerMark(player, "@@rmt__jiechang2", 1)
            can_trigger = true
            search = false
          elseif search and move.from and move.from ~= player then
            player.room:setPlayerMark(player, "@@rmt__jiechang2", 0)
            search = false
          end
        end
      end, nil, Player.HistoryGame)
      return can_trigger
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local slash = Fk:cloneCard("slash")
    local targets = table.filter(room:getAllPlayers(), function (p)
      return not player:isProhibited(p, slash)
    end)
    if #targets < 2 then return false end
    local to = room:askToChoosePlayers(player, {
      min_num = 2,
      max_num = 2,
      targets = targets,
      skill_name = skel.name,
      prompt = "#rmt__jiechang_active2",
      cancelable = true,
    })
    if #to == 2 then
      event:setCostData(self, {tos = to})
      return true
    else
      player.room:setPlayerMark(player, "@@rmt__jiechang2", 0)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.room:setPlayerMark(player, "@@rmt__jiechang2", 0)
    local to = event:getCostData(self).tos
    player:turnOver()
    if not player.dead then
      room:useVirtualCard("slash", nil, player, to, skel.name, true)
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__jiechang"] = "接昌",
  [":rmt__jiechang"] = "一名角色弃牌阶段开始/结束时，若最后获得/失去牌的角色为你，你可以翻面并视为对至多两名角色使用【无中生有】/【杀】。",

  ["@@rmt__jiechang1"] = "接昌 无中",
  ["@@rmt__jiechang2"] = "接昌 杀",
  ["#rmt__jiechang_active1"] = "接昌：是否视为对至多两名角色使用【无中生有】？",
  ["#rmt__jiechang_active2"] = "接昌：是否视为对至多两名角色使用【杀】？",

  ["$rmt__jiechang1"] = "都下业重，大梦安觉，掷枕起、扫平天纲！",
  ["$rmt__jiechang2"] = "直驱秣陵，贼陈朱雀，引刃战、勋满宣阳！",
}

return skel
